﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CONTROLLER;
using ENTITY;
using DevExpress.XtraEditors;

namespace frm_view
{
    public partial class frmMuc : Form
    {
        bool IsInsert = false;
        void KhoaDieuKhien()
        {
            txtMaMuc.Enabled = false;
            txtNoiDung.Enabled = false;
            txtTenMuc.Enabled = false;
            txtVideo.Enabled = false;
            cbbBai.Enabled = false;

            btnSave.Enabled = false;
            btnDelete.Enabled = true;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = true;
        }

        void MoDieuKhien()
        {
            txtMaMuc.Enabled = true;
            txtNoiDung.Enabled = true;
            txtTenMuc.Enabled = true;
            txtVideo.Enabled = true;
            cbbBai.Enabled = true;

            btnSave.Enabled = true;
            btnDelete.Enabled = false;
            btnInsert.Enabled = false;
            btnUpdate.Enabled = false;
        }
        ctrls_Bai obj_Bai = new ctrls_Bai();
        void Refesh()
        {
            cbbBai.Properties.DataSource = obj_Bai.Bai_SelectSQL("Select MaBai,TenBai From tblBai Where MaBai = MaBai");
            cbbBai.Properties.DisplayMember = "MaBai";
            cbbBai.Properties.ValueMember = "TenBai";
            msds.DataSource = obj_Muc.Muc_SelectALL();
        }
        public frmMuc()
        {
            InitializeComponent();
        }
        ctrls_Muc obj_Muc = new ctrls_Muc();
        EN_Muc obj_en_Muc = new EN_Muc();
        private void ViewBai_Load(object sender, EventArgs e)
        {
            Refesh();
            KhoaDieuKhien();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            IsInsert = true;
            MoDieuKhien();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            IsInsert = false;
            MoDieuKhien();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("Bạn đồng ý xóa ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                try
                {
                    obj_en_Muc.MaMuc = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[1]).ToString();
                    obj_Muc.Muc_Delete(obj_en_Muc);
                    XtraMessageBox.Show("Đã xóa thành công","Thông báo");
                    Refesh();
                }
                catch
                {
                    
                    XtraMessageBox.Show("Kiểm tra lại lựa chọn","Thông báo");
                }
            }
        }

        private void msds_Click(object sender, EventArgs e)
        {
            try
            {
                cbbBai.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[0]).ToString();
                txtMaMuc.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[1]).ToString();
                txtTenMuc.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[2]).ToString();
                txtNoiDung.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[3]).ToString();
            }
            catch
            {

                return;
            }
        }

        private void btnLink_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            //ofd.Filter = "Text File (*.rtf)|*.rtf";
            if (ofd.ShowDialog() == DialogResult.OK)
                txtNoiDung.Text = @"\" + ofd.FileName.Substring(ofd.FileName.LastIndexOf("\\") + 1); ;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Error.ClearErrors();

            obj_en_Muc.MaBai = cbbBai.Text;
            obj_en_Muc.MaMuc = txtMaMuc.Text;
            obj_en_Muc.TenMuc = txtTenMuc.Text;
            obj_en_Muc.NoiDug = txtNoiDung.Text;
            obj_en_Muc.Video = txtVideo.Text;
            if (string.IsNullOrEmpty(cbbBai.Text))
                Error.SetError(cbbBai, "Lựa chọn \"CHƯƠNG\"");
            else if (string.IsNullOrEmpty(txtMaMuc.Text))
                Error.SetError(txtMaMuc, "Nhập thông tin \"MÃ BÀI\"");
            else if (string.IsNullOrEmpty(txtTenMuc.Text))
                Error.SetError(txtTenMuc, "Nhập thông tin \"TÊN BÀI\"");
            else if (string.IsNullOrEmpty(txtNoiDung.Text))
                Error.SetError(txtNoiDung, "Nhập thông tin \"NỘI DUNG\"");
            else if (IsInsert == true)
            {

                obj_Muc.Muc_Insert(obj_en_Muc);
                XtraMessageBox.Show("Thêm thành công", "Thông báo");
                Refesh();
                KhoaDieuKhien();
            }
            else
            {
                obj_Muc.Muc_Update(obj_en_Muc);
                XtraMessageBox.Show("Cập nhật thành công", "Thông báo");
                Refesh();
                KhoaDieuKhien();
            }
        }
        public event EventHandler CloseMuc;
        private void frmBai_FormClosing(object sender, FormClosingEventArgs e)
        {
            CloseMuc(this, new EventArgs());
        }

        private void btnVideo_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
                txtVideo.Text = @"\" + ofd.FileName.Substring(ofd.FileName.LastIndexOf("\\") + 1);
        }
    }
}
